package com.xhsj.user.hr.process.hrChangeSalary.web;

import com.xhsj.user.hr.process.dto.hrChangeSalary.MyChangeSalaryApplyListDto;
import com.xhsj.user.hr.process.dto.hrChangeSalary.ReturnChangeSalaryApplyDto;
import com.xhsj.user.hr.process.dto.hrChangeSalary.RunChangeSalaryApplyListDto;
import com.xhsj.user.hr.process.dto.hrTransferJob.ReturnTransferJobApplyDto;
import com.xhsj.user.hr.process.dto.processDefinition.HandleAuditDto;
import com.xhsj.user.hr.process.hrChangeSalary.query.CompletedChangeSalaryApplyQuery;
import com.xhsj.user.hr.process.hrChangeSalary.query.MyChangeSalaryApplyDetailedListQuery;
import com.xhsj.user.hr.process.hrChangeSalary.query.MyChangeSalaryApplyListQuery;
import com.xhsj.user.hr.process.hrChangeSalary.query.RunChangeSalaryApplyListQuery;
import com.xhsj.user.hr.process.message.service.TbProjectMessageService;
import com.xhsj.user.hr.process.query.ApprovalQuery;
import com.xhsj.user.utils.Message;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;
import java.util.concurrent.CompletableFuture;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;


import com.xhsj.user.hr.process.hrChangeSalary.service.TbHrApprovalChangeSalaryService;
import com.xhsj.user.hr.process.hrChangeSalary.entity.TbHrApprovalChangeSalary;

import javax.validation.Valid;


/**
 * <p>
 * hr流程_调薪申请 前端控制器
 * </p>
 *
 * @author liuqinghua
 * @since Sep 14, 2020 10:55:13 AM
 */
@Slf4j
@Api(tags = "hr流程_调薪申请")
@RestController
@RequestMapping("/hrChangeSalary/tbHrApprovalChangeSalary")
public class TbHrApprovalChangeSalaryController {

	@Autowired
    public TbHrApprovalChangeSalaryService service;

	// 项目消息 服务实现类
	@Autowired
	public TbProjectMessageService tbProjectMessageService;


	/**
	 * 保存/修改
	 * @author liuqinghua
	 * @date Sep 14, 2020 10:55:13 AM
	 */
	@ApiOperation(value = "hr流程_调薪申请,保存/修改", notes = "hr流程_调薪申请,保存和修改方法")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:change:salary:edit')")
	public Message save(@RequestBody TbHrApprovalChangeSalary data){
    	try {
    		service.save(data);
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
        	return Message.error(1,"失败");
		}

	}

	/**
	 * 通过id删除数据
	 * @author liuqinghua
	 * @date Sep 14, 2020 10:55:13 AM
	 */
	@ApiOperation(value = "hr流程_调薪申请删除", notes = "通过id删除hr流程_调薪申请信息。")
	@RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:hr:approval:change:salary:delete')")
	public Message delete(@RequestParam(value="id") Long id) {
		try {
			service.removeById(id);
        	return Message.success("成功");
		} catch (Exception e) {
			log.error("删除失败:{}",e);
            return Message.error(1,"失败");
		}

	}

	/**
	 * 批量删除
	 * @author liuqinghua
	 * @date Sep 14, 2020 10:55:13 AM
	 */
	@ApiOperation(value = "批量删除", notes = "批量删除。")
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('tb:hr:approval:change:salary:delete')")
	public Message batchDelete(@RequestBody List<Long> list) {
		try {
			service.removeByIds(list);
    		return Message.success("成功");
		} catch (Exception e) {
    		log.error("删除失败:{}",e);
    		return Message.error(1,"失败");
		}
    }

    /**
    * 分页查询数据
    * @author liuqinghua
    * @date Sep 14, 2020 10:55:13 AM
    */
	@ApiOperation(value = "分页查询hr流程_调薪申请list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
	@RequestMapping(value="/findList",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:change:salary:view')")
	public Message findList(@RequestBody Map<String,Object> data) {
        try {
        	IPage<TbHrApprovalChangeSalary> selectPage = service.selectPage(data);
    		return Message.success("成功",selectPage.getRecords(),selectPage.getTotal(),selectPage.getPages());
        } catch (Exception e) {
			log.error("查询异常:{}",e);
    		return Message.error(1,"失败");
        }
	}

    /**
    * 查询所有数据
    * @author liuqinghua
    * @date Sep 14, 2020 10:55:13 AM
    */
	@ApiOperation(value = "查询hr流程_调薪申请所有数据list", notes = "查询hr流程_调薪申请所有数据list。")
	@RequestMapping(value="/findAll",method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:hr:approval:change:salary:view')")
	public Message findAll(){
        return Message.success("成功",service.selectAll());
	}

    /**
    * 通过id获取实体类
    * @author liuqinghua
    * @date Sep 14, 2020 10:55:13 AM
    */
	@ApiOperation(value = "通过id获取hr流程_调薪申请实体类", notes = "通过id获取hr流程_调薪申请实体类。")
	@RequestMapping(value="/getById",method = RequestMethod.GET)
	public Message getById(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

    /**
    * 通过id查询详细信息
    * @author liuqinghua
    * @date Sep 14, 2020 10:55:13 AM
    */
	@ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
	@RequestMapping(value="/detail",method = RequestMethod.GET)
	public Message detail(@RequestParam(value="id") Long id) {
    	return Message.success("成功",service.getById(id));
	}

	/**
	 * 我的调薪申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的调薪申请列表", notes = "我的调薪申请列表")
	@PostMapping(value="/myChangeSalaryApplyList")
	public Message myChangeSalaryApplyList(@RequestBody @Valid MyChangeSalaryApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("我的调薪申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<MyChangeSalaryApplyListQuery> myChangeSalaryApplyListQueryPage = service.myChangeSalaryApplyList(data);
		return Message.success("成功",myChangeSalaryApplyListQueryPage.getRecords(),myChangeSalaryApplyListQueryPage.getTotal(),myChangeSalaryApplyListQueryPage.getPages());
	}


	/**
	 * 待审核调薪申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "待审核调薪申请列表", notes = "待审核调薪申请列表")
	@PostMapping(value="/runChangeSalaryApplyList")
	public Message runChangeSalaryApplyList(@RequestBody @Valid RunChangeSalaryApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("待审核调薪申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<RunChangeSalaryApplyListQuery> myChangeSalaryApplyListQueryPage = service.runChangeSalaryApplyList(data);
		return Message.success("成功",myChangeSalaryApplyListQueryPage.getRecords(),myChangeSalaryApplyListQueryPage.getTotal(),myChangeSalaryApplyListQueryPage.getPages());
	}

	/**
	 * 已审核调薪申请列表
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "已审核调薪申请列表", notes = "已审核调薪申请列表")
	@PostMapping(value="/completedChangeSalaryApplyList")
	public Message completedChangeSalaryApplyList(@RequestBody @Valid RunChangeSalaryApplyListDto data, BindingResult bindingResult) throws Exception {
		log.info("已审核调薪申请列表参数{}", data);
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		IPage<CompletedChangeSalaryApplyQuery> completedChangeSalaryApplyList = service.completedChangeSalaryApplyList(data);
		return Message.success("成功",completedChangeSalaryApplyList.getRecords(),completedChangeSalaryApplyList.getTotal(),completedChangeSalaryApplyList.getPages());
	}

	/**
	 * 我的调薪申请详细信息
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "我的调薪申请详细信息", notes = "我的调薪申请详细信息")
	@GetMapping(value="/myChangeSalaryApplyDetailedList")
	public Message myChangeSalaryApplyDetailedList(@RequestParam(value = "dataId") Long dataId) throws Exception {
		log.info("我的调薪申请详细信息参数{}", dataId);
		MyChangeSalaryApplyDetailedListQuery myChangeSalaryApplyListQuery = service.myChangeSalaryApplyDetailedList(dataId);
		return Message.success("成功",myChangeSalaryApplyListQuery);
	}

	/**
	 * hr流程-调薪申请开始
	 * @author liuqinghua
	 * @date Sep 7, 2020 10:42:13 AM
	 */
	@ApiOperation(value = "hr流程-调薪申请开始", notes = "hr流程-调薪申请开始")
	@RequestMapping(value = "/startChangeSalaryApply", method = RequestMethod.POST)
//	@PreAuthorize("hasAuthority('tb:hr:approval:entry:apply:edit')")
	public Message startChangeSalaryApply(@RequestBody TbHrApprovalChangeSalary data){
		try {
			ApprovalQuery approvalQuery = service.startChangeSalaryApply(data);
			//保存消息
			CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
				tbProjectMessageService.sendMessageCommon(approvalQuery);
			});
			return Message.success("成功");
		} catch (Exception e) {
			log.error("保存失败:{}",e);
			return Message.error(1,e.getMessage());
		}
	}

	/**
	 * 调薪流程作废
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "调薪流程作废", notes = "调薪流程作废")
	@PostMapping(value="/approvalChangeSalaryToVoidApply")
	public Message approvalChangeSalaryToVoidApply(@RequestBody @Valid HandleAuditDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		ApprovalQuery approvalQuery = service.approvalChangeSalaryToVoidApply(data);
		return Message.success("成功");
	}

	/**
	 * 调薪流程审批
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "调薪流程审批", notes = "调薪流程审批")
	@PostMapping(value="/approvalChangeSalaryApply")
	public Message approvalChangeSalaryApply(@RequestBody @Valid HandleAuditDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		ApprovalQuery approvalQuery =  service.approvalChangeSalaryApply(data);
		//保存消息
		CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
			tbProjectMessageService.sendMessageCommon(approvalQuery);
		});
		return Message.success("成功");
	}

	/**
	 * 调薪申请重新提交
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "调薪申请重新提交", notes = "调岗申请重新提交")
	@PostMapping(value="/returnChangeSalaryApply")
	public Message returnChangeSalaryApply(@RequestBody @Valid ReturnChangeSalaryApplyDto data, BindingResult bindingResult) throws Exception {
		if (bindingResult.hasErrors()) {
			log.info(bindingResult.getFieldError().getDefaultMessage());
			return Message.error(1,bindingResult.getFieldError().getDefaultMessage());
		}
		ApprovalQuery approvalQuery = service.returnChangeSalaryApply(data);
		//保存消息
		CompletableFuture<Void> f = CompletableFuture.runAsync(() -> {
			tbProjectMessageService.sendMessageCommon(approvalQuery);
		});
		return Message.success("成功");
	}


	/**
	 * 调薪申请删除
	 * @author liuqinghua
	 * @date Sep 1, 2020 10:07:11 AM
	 */
	@ApiOperation(value = "调薪申请删除", notes = "调薪申请删除")
	@GetMapping(value="/deleteChangeSalaryApply")
	public Message deleteChangeSalaryApply(@RequestParam(value = "dataId") Long dataId) throws Exception {
		service.deleteChangeSalaryApply(dataId);
		return Message.success("成功");
	}


}

